package adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.newxlmedical.R;

import java.util.ArrayList;
import java.util.List;

import beans.CardinalNumberModifyBean;
import beans.PutCardinalNumberBean;
import beans.WarningInfoBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import custom.BaseRecyclerAdapter;
import dialogs.StorageWarningModifyDialog;
import netframe.GetRetrofit;
import netframe.NetAddress;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import utils.ToastUtil;

/**
 * Created by Administrator on 2018/11/9.
 */

public class CardinalNumberAdapter extends BaseRecyclerAdapter<CardinalNumberAdapter.CardinalNumberViewHolder> {

    private Context context;
    private List<WarningInfoBean.DataBean> list;

    public CardinalNumberAdapter(Context context, List<WarningInfoBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public CardinalNumberAdapter.CardinalNumberViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.adapter_cardinal_number, parent,false);

        return new CardinalNumberViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(CardinalNumberAdapter.CardinalNumberViewHolder viewHolder, final int position) {
        final WarningInfoBean.DataBean dataBean = list.get(position);
        viewHolder.tvCardinalNumberName.setText(dataBean.getGoodName());
        viewHolder.tvCardinalNumberSpecification.setText(dataBean.getSpecification());
        viewHolder.tvCardinalNumberSuppier.setText(dataBean.getManufacturer());
        viewHolder.tvCardinalNumberNumber.setText(dataBean.getCardinalNumber() + "");

        if (position % 2 == 0) {
            viewHolder.llSearchAdapter.setBackgroundColor(context.getResources().getColor(R.color.partLineBackground));
        } else {
            viewHolder.llSearchAdapter.setBackgroundColor(context.getResources().getColor(R.color.white));
        }

        viewHolder.tvCardinalNumberModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StorageWarningModifyDialog dialog = new StorageWarningModifyDialog(context, R.style.dialog, "修改基数" ,dataBean.getCardinalNumber() + "", new StorageWarningModifyDialog.ModifyInterface() {
                    @Override
                    public void modify(final String number) {
                        final List<PutCardinalNumberBean> modifyList = new ArrayList<>();
                        final PutCardinalNumberBean bean = new PutCardinalNumberBean();
                        bean.setID(dataBean.getGID());
                        bean.setCardinalNumber(Integer.parseInt(number));
                        modifyList.add(bean);

                        GetRetrofit.getInstance().setCardinalNumber(NetAddress.token,NetAddress.machineCode,modifyList)
                                .enqueue(new Callback<CardinalNumberModifyBean>() {
                                    @Override
                                    public void onResponse(Call<CardinalNumberModifyBean> call, Response<CardinalNumberModifyBean> response) {
                                        CardinalNumberModifyBean body = response.body();
                                        if (body != null){

                                            if (body.getResult().equals("OK")) {
                                                ToastUtil.showToast(context,"修改成功!");
                                                list.get(position).setCardinalNumber(Integer.parseInt(number));
                                                notifyDataSetChanged();
                                            }else {
                                                ToastUtil.showToast(context,"修改失败!");
                                            }

                                        }else {
                                            ToastUtil.showToast(context,"修改失败!");
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<CardinalNumberModifyBean> call, Throwable t) {
                                        ToastUtil.showToast(context,"修改失败!");
                                    }
                                });

                    }
                });
                dialog.show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class CardinalNumberViewHolder extends BaseRecyclerAdapter.BaseViewHolder {
        @BindView(R.id.tv_cardinal_number_name)
        public TextView tvCardinalNumberName;
        @BindView(R.id.tv_cardinal_number_specification)
        TextView tvCardinalNumberSpecification;
        @BindView(R.id.tv_cardinal_number_suppier)
        TextView tvCardinalNumberSuppier;
        @BindView(R.id.tv_cardinal_number_number)
        TextView tvCardinalNumberNumber;
        @BindView(R.id.tv_cardinal_number_modify)
        TextView tvCardinalNumberModify;
        @BindView(R.id.ll_search_adapter)
        LinearLayout llSearchAdapter;

        public CardinalNumberViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
